package com.tom.baselibrary.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.tom.baselibrary.R;
import java.lang.ref.WeakReference;

/**
 * 自定义的万能dialog
 * 类的职责，参考系统的AlertDialog结构。主要涉及5个类。
 *
 */
public class AlertDialog extends Dialog {
    private AlertController mAlert;

    public AlertDialog(Context context, int themeResId) {
        super(context, themeResId);
        mAlert = new AlertController(this, getWindow());
    }
    // 设置文本
    public void setText(int viewId, CharSequence text) {
        mAlert.setText(viewId, text);
    }

    // 获取控件的公共方法
    public <T extends View> T getView(int viewId) {
        return mAlert.getView(viewId);
    }

    // 设置点击事件，便于拿到Dialog中输入的内容。
    // 通过dialog，获取EditText 里面的输入内容。通过mAlert来调用。
    public void setOnClickListener(int viewId, View.OnClickListener listener) {
        mAlert.setOnClickListener(viewId, listener);
    }

    public static class Builder {
        private AlertController.AlertParams P;

        public Builder(Context context) {
            this(context, R.style.dialog); // 使用默认主题
        }

        // 用户设置了主题
        public Builder(Context context, int themeResId) {
            P = new AlertController.AlertParams(context, themeResId);
        }
        //region 设置Dialog内容，使用链式调用
        public Builder setContentView(View view) {
            P.mView = view;
            P.mViewLayoutResId = 0;
            return this;
        }
        // 设置布局内容的layoutId，使用自定义layout.xml 作为view
        public Builder setContentView(int layoutId) {
            P.mView = null;
            P.mViewLayoutResId = layoutId;
            return this;
        }

        // 给组件设置文本
        public Builder setText(int viewId, CharSequence text) {
            P.mTextArray.put(viewId, text);
            return this;
        }

        // 给组件设置点击事件, 可能多个点击事件，所以使用集合保存点击事件。
        public Builder setOnClickListener(int viewId, View.OnClickListener listener) {
            P.mOnClickArray.put(viewId, listener);
            return this;
        }

        // 配置一些类似全屏显示的参数。全屏
        public Builder fullWidth() {
            P.mWidth = ViewGroup.LayoutParams.MATCH_PARENT;
            return this;
        }

        // 从底部弹出，isAnimation-是否有动画效果
        public Builder fromBottom(boolean isAnimation) {
            if (isAnimation) {
                P.mAnimations = R.style.dialog_from_bottom_anim;
            }
            P.mGravity = Gravity.BOTTOM; // 底部显示
            return this;
        }

        // 设置Dialog的宽高
        public Builder setWidthAndHeight(int width, int height) {
            P.mWidth = width;
            P.mHeight = height;
            return this;
        }

        // 添加默认动画：缩放动画
        public Builder addDefaultAnimation() {
            P.mAnimations = R.style.dialog_scale_anim;
            return this;
        }
        // 设置自定义的动画效果
        public Builder setAnimations(int styleAnimation) {
            P.mAnimations = styleAnimation;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            P.mCancelable = cancelable;
            return this;
        }
        public Builder setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
            P.mOnCancelListener = onCancelListener;
            return this;
        }
        public Builder setOnDismissListener(DialogInterface.OnDismissListener dismissListener) {
            P.mOnDismissListener = dismissListener;
            return this;
        }
        public Builder setOnKeyListener(DialogInterface.OnKeyListener onKeyListener) {
            P.mOnKeyListener = onKeyListener;
            return this;
        }
        //endregion


        //region Dialog create/show
        public AlertDialog create() {
            final AlertDialog dialog = new AlertDialog(P.mContext, P.mThemeResId);
            P.apply(dialog.mAlert);
            dialog.setCancelable(P.mCancelable); //点击空白区域，是否可以取消dialog
            if (P.mCancelable) {
                dialog.setCanceledOnTouchOutside(true);
            }
            dialog.setOnCancelListener(P.mOnCancelListener); // cancel监听
            dialog.setOnDismissListener(P.mOnDismissListener);
            if (P.mOnKeyListener != null) {
                dialog.setOnKeyListener(P.mOnKeyListener);
            }
            return dialog;
        }

        public AlertDialog show() {
            final AlertDialog dialog = create();
            dialog.show();
            return dialog;
        }
        //endregion
    }

/*    private void testMyDialog() {
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setContentView(R.layout.dialog_comment_dialog)
                .setText(R.id.btn_go, "提交按钮--")
                .fromBottom(true)
                .show();
        // dialog的点击事件
        final EditText commentEt = dialog.getView(R.id.et_input);
        commentEt.setHint("请文明评论~~");
        // 弹框中，如果有ListView，RecyclerView，CheckBox等特有的对象，要用getView方法，先得到对象。然后再做操作。
        dialog.setOnClickListener(R.id.account_icon_weibo, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = commentEt.getText().toString();
                Toast.makeText(MainActivity.this, "click weibo:" + input, Toast.LENGTH_LONG).show();
            }
        });
    }*/
}

